class Gamer < Player

  attr_accessor :isAttacking

  def initialize (name = 'TEMP')
    super(name)
    self.isAttacking = true
  end

  def get_list_of_defending_cards(card)
    mark_defending_cards(card)
    return @source.select { |item| item.enabled? }
  end

  def sortCards
    @source.sort_by! { |field| -field.value } #v1
    #::v2 sorting:: @source.sort_by! { |field| [field.suit, -field.rank] }# asc suit -> descending rank
  end

  def mark_cards(tableCards)
    if isAttacking
        highlight_attacking_cards(tableCards)
    else
        opponentAttackingCard = tableCards.last
        puts opponentAttackingCard.nil?, tableCards
        mark_defending_cards(opponentAttackingCard)
    end
  end

  def check_is_valid_answer(selectedCard, tableCards)
    # check is user own card
    index = @source.index { |card| card.serialize == selectedCard }
    if  index.nil?
      return nil
    end
    # verify is correct for answer card
    mark_cards(tableCards)

    card = @source[index]
    return card.enabled? ? card : nil
  end


  private
  def mark_defending_cards(opponentAttackingCard)
    @source.each { |item| item.enabled = opponentAttackingCard.compareIsStronger(item) }
  end

end